package com.ht.activiti.controller;

import com.ht.activiti.model.ActivitiSaveModel;
import com.ht.activiti.service.ImageService;
import com.ht.activiti.util.ActivitiTracingChart;

import java.io.*;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.HistoryService;
import org.activiti.engine.ManagementService;
import org.activiti.engine.RuntimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

/**
 * @author henry
 */
@RestController
@RequestMapping("activiti")
public class ActivitiController {

    @Resource
    private ActivitiTracingChart activitiTracingChart;

    @Resource
    private ManagementService managementService;

    @Resource
    private HttpServletResponse response;

    @Resource
    private RuntimeService runtimeService;

    @Resource
    private HistoryService historyService;

    @Autowired
    private ImageService imageService;

    /**
     * 这个方法没有用 不用管
     * @param request
     * @param modelId
     * @param activitiSaveModel
     */
    @PutMapping(value = "/{modelId}/save")
    @ResponseStatus(value = HttpStatus.OK)
    public void saveModel(HttpServletRequest request, @PathVariable String modelId, ActivitiSaveModel activitiSaveModel) {
        activitiSaveModel.setModelId(modelId);
        System.out.println("save it");
    }

    @GetMapping("/getFlowcChart")
    public void getFlowcChart(String processInstanceId, HttpServletResponse response) throws IOException {
        activitiTracingChart.generateFlowChart(processInstanceId, response.getOutputStream());
    }

    /**
     * 根据实例id获取高亮显示的流程图
     * @param instanceId 实例 id
     * @param response response
     *
     */
    @RequestMapping(value = "/viewProcessImg", method = RequestMethod.GET)
    @ResponseBody
    @CrossOrigin
    public void viewProcessImg(@RequestParam("instanceId") String instanceId, HttpServletResponse response)  {
        OutputStream os = null;
        try {
            String directory = "F:" + File.separator + "temp" + File.separator;
            final String suffix = ".png";
            /*File folder = new File(directory);
            File[] files = folder.listFiles();
            for (File file : files) {
                if (file.getName().equals(instanceId + suffix)) {
                    file.delete();
                }
            }*/

            byte[] processImage = imageService.getFlowImgByProcInstId(instanceId);

           /* File dest = new File(directory + instanceId + suffix);
            os = new FileOutputStream(dest, true);
            os.write(processImage, 0, processImage.length);
            os.flush();*/

            response.setContentType("image/png");
            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.write(processImage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
